using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web.Core;
using Web.Core.Objects;

public partial class AdminCP_Products : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Language"] == null)
            Session["Language"] = Web.Core.GlobalStorage.Language.Vietnamese;

        int total = 1; int page = 1;
        if (Request.Params["page"] != null)
            page = int.Parse(Request.Params["page"]);

        ArrayList arrProducts;
        if (Request.Params["act"] != "search")
        {
            arrProducts = Products.GetAllProducts((int)Session["Language"], page, GlobalStorage.productsPerPageForAdmin);
            total = Products.CountAllProducts((int)Session["Language"]);
        }
        else
        {
            //hien thi phan search
            string name = "";
            int man = 0, cat = 0;
            string type = "";
            string visible = "";
            if (Request.Params["n"] != null)
                name = Request.Params["n"];
            if (Request.Params["m"] != null)
                man = int.Parse(Request.Params["m"]);
            if (Request.Params["c"] != null)
                cat = int.Parse(Request.Params["c"]);
            if (Request.Params["s"] != null)
                type = Request.Params["s"];
            if (Request.Params["v"] != null)
                visible = Request.Params["v"];
            arrProducts = Products.GetAllProducts((int)Session["Language"], page, GlobalStorage.productsPerPageForAdmin, name, man, cat, Utils.GetVisible(visible), Utils.GetProSType(type));
            total = Products.CountAllProducts((int)Session["Language"], name, man, cat, Utils.GetVisible(visible), Utils.GetProSType(type));
        }
        arrProducts = AdjustmentDescription(arrProducts, 20);
        if (!IsPostBack)
        {
            this.rptProducts.DataSource = arrProducts;
            this.rptProducts.DataBind();
            InitControl();
        }
        // Pager
        this.Pager1.objectsPerPage = GlobalStorage.productsPerPageForAdmin;
        this.Pager1.displayAll = false; // Chi hien thi ben phai
        this.Pager1.totalObjects = total;
        // ---------------        
        if (Request.Params["do"] != null)
        {
            string pDo = Request.Params["do"];
            switch (pDo)
            {
                case "edit":
                    if (Request.Params["pid"] != null)
                        LoadControls();
                    break;
                case "add":
                    LoadControls();
                    break;
                case "del":
                    if (Request.Params["pid"] != null)
                    {
                        int proID = 0;
                        try
                        {
                            proID = int.Parse(Request.Params["pid"].ToString());
                        }
                        catch { }
                        Products.DeleteProduct(new Product(proID, 0, 0, 0, "", "", 0, true, true));
                        this.PreRenderComplete += new EventHandler(AdminCP_Products_PreRenderComplete);

                    }
                    break;
                default:
                    break;
            }
        }
    }

    void AdminCP_Products_PreRenderComplete(object sender, EventArgs e)
    {
        Response.Redirect("Products.aspx");
    }

    ArrayList AdjustmentDescription(ArrayList arr, int len)
    {
        foreach (Product pro in arr)
        {
            if (pro.Description.Length > len)
                pro.Description = pro.Description.Substring(0, len) + " ...";
        }
        return arr;
    }
    void InitControl()
    {
        this.btsearch.Text = Resources.Lang.Search;
        this.drpSVisible.Items.Clear();
        this.drpSSpecial.Items.Clear();
        this.drpSMan.Items.Clear();
        this.drpSCat.Items.Clear();

        this.drpSVisible.Items.Add("");
        this.drpSVisible.Items.Add(Resources.Lang.Yes);
        this.drpSVisible.Items.Add(Resources.Lang.No);
        this.drpSSpecial.Items.Add("");
        this.drpSSpecial.Items.Add(Resources.Lang.Yes);
        this.drpSSpecial.Items.Add(Resources.Lang.No);

        this.drpSCat.Items.Add("");
        foreach (Category cat in Categories.GetCategories((int)Session["Language"]))
            this.drpSCat.Items.Add("(" + cat.Id + ")  " + cat.Name);
        this.drpSMan.Items.Add("");
        foreach (Manufacturer man in Manufacturers.GetAllManufacturers())
            this.drpSMan.Items.Add("(" + man.MID + ")  " + man.MName);
    }
    void LoadControls()
    {
        Control addEditProducts = (Control)Page.LoadControl("~/AdminCP/AdminControls/AddEditProduct.ascx");
        this.PlaceHolderBottom.Controls.Add(addEditProducts);
    }
    protected void btsearch_Click(object sender, EventArgs e)
    {
        Response.Redirect(HandleUrl());
    }
    //string GetUrl()
    //{
    //    string u = "";
    //    if (Request.Params["act"] != null)
    //    {
    //        int q = Request.Url.ToString().IndexOf('?') + 1;
    //        if (q > 0)
    //            u = Request.Url.ToString().Substring(q) + "&";
    //        u = u.Replace("do=add", "");
    //        u = u.Replace("do=edit", "");
    //        u = u.Replace("do=del", "");            
    //        if (u.Contains("&pid=") == true)
    //        {
    //            int f = u.IndexOf("&pid");
    //            int l = u.IndexOf("&", f + 5);
    //            u = u.Remove(f, l - f);
    //        }
    //        u = u.Replace("&&", "&");
    //    }
    //    return u;
    //}
    string HandleUrl()
    {
        string url = "Products.aspx?act=search";
        if (txtSName.Text != "")
            url += "&n=" + txtSName.Text;
        if (drpSMan.SelectedValue != "")
            url += "&m=" + Utils.GetID(drpSMan.SelectedValue);
        if (drpSCat.SelectedValue != "")
            url += "&c=" + Utils.GetID(drpSCat.SelectedValue);
        if (drpSSpecial.SelectedValue != "")
            url += "&s=" + drpSSpecial.SelectedValue;
        if (drpSVisible.SelectedValue != "")
            url += "&v=" + drpSVisible.SelectedValue; ;
        return url;
    }
}
